﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DCTLib.Common;
using System.Data;

namespace DCTAccess.Business.Location
{
    public class LocationFilter
    {
        private int _locationID = -1;
        private string _locationName = string.Empty;
        private string _locationAlias = string.Empty;
        private bool _isEnable = false;
        private int _parentID = -1;

        public int LocationID
        {
            get { return _locationID; }
            set { _locationID = value; }
        }

        public string LocationName
        {
            get { return _locationName; }
            set { _locationName = value; }
        }

        public string LocationAlias
        {
            get { return _locationAlias; }
            set { _locationAlias = value; }
        }

        public bool IsEnable
        {
            get { return _isEnable; }
            set { _isEnable = value; }
        }

        public int ParentID
        {
            get { return _parentID; }
            set { _parentID = value; }
        }

        private string SelectAllSQL
        {
            get
            {
                StringBuilder sql = new StringBuilder();
                sql.Append("SELECT LocationID, LocationName, LocationAlias, ParentID, IsEnable ");
                sql.Append("FROM Location ");
               //sql.Append("select *, p.LocationName as ParentName from Location left join Location p on Location.ParentID = p.LocationID");
                return sql.ToString();
            }
        }

        public DataTable GetLocation()
        {
            SQLHelper sqlHelper = new SQLHelper();
            sqlHelper.SwitchCommandType(CommandType.Text);

            StringBuilder sql = new StringBuilder(SelectAllSQL);

            if (LocationID != -1)
            {
                sql.Append("WHERE " + IntegerCompareSQLBuilder.GetSQL("LocationID", IntegerCompareTypes.Equals, "@LocationID"));
                sqlHelper.AddParameter("@LocationID", LocationID);
            }

            return sqlHelper.GetDataTable(sql.ToString() + " ORDER BY LocationName ASC  ");
        }
    }
}
